local ma__zhonggua = fk.CreateSkill {
  name = "ma__zhonggua",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["ma__zhonggua"] = "种瓜",
  [":ma__zhonggua"] = "锁定技，你使用手牌时摸X张牌（X为该牌在你手中的轮数-1）；你手牌中最早获得的牌不计入手牌上限。",
  ["@ma__zhonggua-inhand"] = "瓜",

  ["$ma__zhonggua1"] = "",
  ["$ma__zhonggua2"] = "",
}

ma__zhonggua:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.to == player and player:hasSkill(ma__zhonggua.name) and move.toArea == Card.PlayerHand then
        return true
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and player:hasSkill(ma__zhonggua.name) and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(player:getCardIds("h"), info.cardId) then
            room:setCardMark(Fk:getCardById(info.cardId), "ma__zhonggua-inhand", room:getBanner("RoundCount"))
          end
        end
      end
    end
  end,
})

ma__zhonggua:addEffect(fk.GameStart, {
  can_refresh = Util.TrueFunc,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if player:hasSkill(ma__zhonggua.name) and not player:isKongcheng() then
      for _, id in ipairs(player:getCardIds("h")) do
        room:setCardMark(Fk:getCardById(id), "ma__zhonggua-inhand", 1)
      end
    end
  end,
})

ma__zhonggua:addEffect(fk.RoundStart, {
  can_refresh = Util.TrueFunc,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    if player:hasSkill(ma__zhonggua.name) and not player:isKongcheng() then
      for _, id in ipairs(player:getCardIds("h")) do
        local card = Fk:getCardById(id)
        if card:getMark("ma__zhonggua-inhand") > 0 and card:getMark("ma__zhonggua-inhand") < player.room:getBanner("RoundCount") then
          local n = player.room:getBanner("RoundCount") - card:getMark("ma__zhonggua-inhand")
          room:setCardMark(card, "@ma__zhonggua-inhand", n)
        end
      end
    end
  end,
})

ma__zhonggua:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(ma__zhonggua.name) and
    data.card:getMark("ma__zhonggua-inhand") > 0 and
      #Card:getIdList(data.card) == 1 and
      table.contains(player:getCardIds("h"), Card:getIdList(data.card)[1]) and
      Fk:getCardById(Card:getIdList(data.card)[1]):getMark("ma__zhonggua-inhand") < player.room:getBanner("RoundCount")
  end,
  on_refresh = function (self, event, target, player, data)
    local num = Fk:getCardById(Card:getIdList(data.card)[1]):getMark("@ma__zhonggua-inhand")
    player:drawCards(num, ma__zhonggua.name)
  end,
})

ma__zhonggua:addAcquireEffect(function (self, player, is_start)
  if player:hasSkill(ma__zhonggua.name) and not is_start then
    local room = player.room
    if not player:isKongcheng() then
      for _, id in ipairs(player:getCardIds("h")) do
        local card = Fk:getCardById(id)
        if card:getMark("ma__zhonggua-inhand") == 0 then
          room:setCardMark(card, "ma__zhonggua-inhand", room:getBanner("RoundCount"))
          if card:getMark("ma__zhonggua-inhand") < player.room:getBanner("RoundCount") then
            local n = player.room:getBanner("RoundCount") - card:getMark("ma__zhonggua-inhand")
            room:setCardMark(card, "@ma__zhonggua-inhand", n)
          end
        end
      end
    end
  end
end)

ma__zhonggua:addEffect("maxcards", {
  exclude_from = function(self, player, card)
    return player:hasSkill(ma__zhonggua.name) and
    table.every(player:getCardIds("h"), function(id)
      return Fk:getCardById(id):getMark("@ma__zhonggua-inhand") <= card:getMark("@ma__zhonggua-inhand")
    end)
  end,
})

return ma__zhonggua